// 路由核心文件
import Vue from 'vue'	//引入Vue
import Router from 'vue-router'		//引入vue-router
import Hello from '@/components/Hello'		//引入根目录下的Hello.vue组件
import Home from '@/views/home/index'
import Dashboard from '@/views/dashboard/index'
import Data from '@/views/data/index'
import Login from '@/views/login/index'
import Error from '@/components/Error'
import Hi from '@/components/Hi'
import Hi1 from '@/components/Hi1'
import Hi2 from '@/components/Hi2'

import menuModule from '@/store/modules/menu'
// import menuModule from 'vuex-store/modules/menu'

Vue.use(Router)		//Vue全局使用Router

export default new Router({
	mode: 'history', // Demo is living in GitHub.io, so required!
	linkActiveClass: 'is-active',
	scrollBehavior: () => ({ y: 0 }),
	routes: [
		{
			path: '*',
			component: Error
		},
		{
			path: '/',
			name: 'Home',
			component: Home
		},
		{
			path: '/login',
			name: 'Login',
			component: Login
		},
		{
			path: '/dashboard',
			name: '控制台',
			component: Dashboard,
			children: [
				{
					path: '/data',
					name: '数据浏览',
					component: require('../views/data/index')
				}
			]
		},
		{
			path: '/hi',
			component: Hi,
			children: [
				{ path: '/', component: Hi },
				{ path: 'hi1', component: Hi1 },
				{ path: 'hi2', component: Hi2 }
			]
		},
		...generateRoutesFromMenu(menuModule.state.items),
		{
			path: '*',
			redirect: '/'
		}
	]
})


// Menu should have 2 levels.
function generateRoutesFromMenu(menu = [], routes = []) {
	for (let i = 0, l = menu.length; i < l; i++) {
		let item = menu[i]
		if (item.path) {
			routes.push(item)
		}
		if (!item.component) {
			generateRoutesFromMenu(item.children, routes)
		}
	}
	return routes
}